Binary Watch
Question
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads “3:25”.
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
|
|
Analysis
简单的回溯问题,注意以下几点:
- 递归过程中需要标明小时和分钟计算到了哪位数字,即hindex,mindex,否则生成重复时间
- 利用HashSet排除重复的时间
- 在插入过程中,所有h>=12和m>=60的情况都排除
Code
|
|
Letter Combinations of a Phone Number
Question
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Example:
|
|
Analysis
同上简单的回溯问题,利用String数组存储不同数字对应的字符串,注意:
- 确认keys下标的时候 -‘0’
- 初始判断是否digits长度为0
Code
|
|